<?php
namespace App\Services;

use EasyWeChat;
use Illuminate\Support\Facades\Cache;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;

class WechatService
{
    //获取小程序的access_token
    public function getAccessToken()
    {
        //$token = $this->_get_access_token();
        $row = DB::table('wechat_access_token')->orderBy('id','desc')->first();

        if(empty($row) || time() >= $row->expires_in)
        {
            $token = $this->_get_access_token();
            return $token;
        }

        $token = [
            'access_token'  => $row->access_token,
            'expires_in'    => $row->expires_in,
        ];
        return $token;
    }

    private function _get_access_token()
    {
        $miniProgram = EasyWeChat::miniProgram(); // 小程序
        // 获取 access token 实例
        $accessToken = $miniProgram->access_token;
        $token = $accessToken->getToken(); // token 数组  token['access_token'] 字符串

        $expires_in = $token['expires_in'] + time();
        $token['expires_in'] = $expires_in;

        DB::table('wechat_access_token')->insert([
            'access_token' => $token['access_token'],
            'expires_in'   => $expires_in,
        ]);

        return $token;
    }

    public function getQuota()
    {
        $http_client = new Client();

        $access_token = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/express/business/quota/get?access_token='.$access_token['access_token'];

        $body = [
            'delivery_id' => 'ZTO',
            'biz_id' => 'ZTO531558226398055',
        ];
        $response = $http_client->request('POST', $url, [
            'body' => json_encode($body)
        ])->getBody()->getContents();

        return json_decode($response, true);
    }

    public function add_order($body)
    {
        $http_client = new Client();

        $access_token = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/express/business/order/add?access_token='.$access_token['access_token'];
        $response = $http_client->request('POST', $url, [
            'body' => json_encode($body,JSON_UNESCAPED_UNICODE)
        ])->getBody()->getContents();

        return json_decode($response, true);
    }

    public function updatePrinter($openid, $update_type = 'bind')
    {
        $http_client = new Client();

        $body = [
            'openid'        => $openid,
            'update_type'   => $update_type
        ];
        $access_token = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/express/business/printer/update?access_token='.$access_token['access_token'];
        $response = $http_client->request('POST', $url, [
            'body' => json_encode($body)
        ])->getBody()->getContents();

        return json_decode($response, true);
    }

    public function getPrinter()
    {
        $http_client = new Client();

        $access_token = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/express/business/printer/getall?access_token='.$access_token['access_token'];
        $response = $http_client->request('GET', $url)->getBody()->getContents();

        return json_decode($response, true);
    }

    public function getExpressPath($openid, $order_id, $waybill_id, $delivery_id = 'ZTO')
    {
        $http_client = new Client();

        $body = [
            'openid'        => $openid,
            'order_id'      => $order_id,
            'waybill_id'    => $waybill_id,
            'delivery_id'   => $delivery_id
        ];
        $access_token = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/express/business/path/get?access_token='.$access_token['access_token'];
        $response = $http_client->request('POST', $url, [
            'body' => json_encode($body,JSON_UNESCAPED_UNICODE)
        ])->getBody()->getContents();

        return json_decode($response, true);
    }

    public function getOrder($order_id, $openid, $waybill_id, $delivery_id = 'ZTO')
    {
        $http_client = new Client();

        $body = [
            'openid'        => $openid,
            'order_id'      => $order_id,
            'waybill_id'    => $waybill_id,
            'delivery_id'   => $delivery_id
        ];
        $access_token = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/express/business/order/get?access_token='.$access_token['access_token'];
        $response = $http_client->request('POST', $url, [
            'body' => json_encode($body,JSON_UNESCAPED_UNICODE)
        ])->getBody()->getContents();

        return json_decode($response, true);
    }

    public function cancelOrder($order_id, $openid, $waybill_id, $delivery_id = 'ZTO')
    {
        $http_client = new Client();

        $body = [
            'openid'        => $openid,
            'order_id'      => $order_id,
            'waybill_id'    => $waybill_id,
            'delivery_id'   => $delivery_id
        ];
        $access_token = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/express/business/order/cancel?access_token='.$access_token['access_token'];
        $response = $http_client->request('POST', $url, [
            'body' => json_encode($body,JSON_UNESCAPED_UNICODE)
        ])->getBody()->getContents();

        return json_decode($response, true);
    }
}
